package cn.kgc.systemweb.controller;

import cn.kgc.bean.House;
import cn.kgc.bean.System;
import cn.kgc.service.SystemService;
import cn.kgc.utils.ResultEntity;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@CrossOrigin
public class SystemController {
    @Reference
    SystemService systemService;

    @RequestMapping("/system/login")
    @ResponseBody
    public ResultEntity login(
            @RequestParam(required = false,defaultValue = "") String username ,
            @RequestParam(required = false,defaultValue = "") String password,
            Model model,
            Map<String,Object> map){

        System login = systemService.login(username, password);
        if (login==null){
            map.put("result","null");
        }else{
            map.put("result","success");
        }
        return ResultEntity.successWithoutData(map);
    }

    @RequestMapping("/house")
    public String tomain(){
        return "system-main";
    }


    @GetMapping("/query/house")
    public ResultEntity<PageInfo<House>> queryHouse(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum
            ,@RequestParam(value = "keyword",defaultValue = "") String keyword
    ){
        PageInfo<House> pageInfo = systemService.selectAll(pageNum, keyword);
        return ResultEntity.successWithoutData(pageInfo);
    }


    //添加
    @RequestMapping(value = "/save")
    public  ResultEntity<String> insert(@RequestBody House house){
        try{
            Integer save=0;
            if (house.getId()==null){
              save= systemService.saveHouse(house);
            }else {
                save=systemService.updateHouse(house);
            }
            return ResultEntity.successWithoutData(save+"");
        }catch (Exception e){
            return ResultEntity.failed(e.getMessage());
        }
    }

    //根据id查询
    @RequestMapping(value = "/selectById")
    public ResultEntity<House> selectById(Integer id){
        House house = systemService.selectAllById(id);
        return ResultEntity.successWithoutData(house);
    }

    //删除
    @RequestMapping(value = "/delHouse")
    public ResultEntity<String> delHouse(Integer id){
        try {
            Integer delHouse = systemService.delHouse(id);
            return ResultEntity.successWithoutData(delHouse+"");
        }catch (Exception ex){
            return ResultEntity.failed(ex.getMessage());
        }


    }

}
